﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

namespace Yoozoo.Gameplay.Liberty.World
{
    public class WorldState : MonoBehaviour
    {
        public static WorldState Ws;

        [Header("路径长度 / npcUnitLength = 可行走Npc数量上限")]
        [Range(1,100)]
        public float npcUnitLength = 20f;
        public List<NpcPathState> npcPathStates;

        public PlayerWorldState playerWorldState;

        private void Awake()
        {
            Ws = this;
            npcPathStates = new List<NpcPathState>(20);
        }


        private void FixedUpdate()
        {
            
        }


        public NpcPathState AddNpcPathState(int pathId)
        {
            NpcPathState pathState = 
                new GameObject("NpcPathState_" + pathId).AddComponent<NpcPathState>();
            pathState.pathId = pathId;
            pathState.npcIdList = new List<int>(10);
            npcPathStates.Add(pathState);
            pathState.transform.parent = transform;
            return pathState;
        }
        
        public void AddToPath(int npcId, int pathId)
        {
            var pathState = GetNpcPathState(pathId);
            if (pathState!=null)
            {
                if (pathState.npcIdList==null)
                {
                    pathState.npcIdList = new List<int>(10);
                }
                pathState.npcIdList.Add(npcId);
            }
        }

        public void RemoveFromPath(int npcId, int pathId)
        {
            var pathState = GetNpcPathState(pathId);
            if (pathState!=null)
            {
                if (pathState.npcIdList!=null && pathState.npcIdList.Contains(npcId))
                {
                    pathState.npcIdList.Remove(npcId);
                }
            }
        }

        public NpcPathState GetNpcPathState(int pathId)
        {
            if (npcPathStates!=null)
            {
                foreach (var s in npcPathStates) 
                {
                    if (s.pathId == pathId)
                    {
                        return s;
                    } 
                }
            }

            return null;
        }



        public void OnDispose()
        {
            
        }
    }
}




